package com.tongfusoft.terminal.utils;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.tongfusoft.base.ExtConstant;
import com.tongfusoft.web.core.data.DictionaryData;
import com.tongfusoft.web.core.data.SystemParamData;

public class ParamUtils {

	public static String getDefaultProjectId() {
		return (String) ServletActionContext.getServletContext().getAttribute(ExtConstant.DEFAULTPROJECT);
	}

	public static String getCurrProjectId() {
		if (StringUtils.isNotEmpty((String) ServletActionContext.getServletContext().getAttribute(
				ExtConstant.CURRPROJECT))) {
			return (String) ServletActionContext.getServletContext().getAttribute(ExtConstant.CURRPROJECT);
		} else {
			return getDefaultProjectId();
		}

	}

	/**
	 * 获取系统参数的值
	 * 
	 * @param sysParams
	 * @param code
	 * @return
	 */
	public static SystemParamData findSystemParamByCode(Map<String, Map<String, SystemParamData>> sysParams, String code) {
		return findSystemParamByCode(sysParams, code, getCurrProjectId());
	}

	/**
	 * 获取系统参数的值
	 * 
	 * @param sysParams
	 * @param code
	 * @param project
	 * @return
	 */
	public static SystemParamData findSystemParamByCode(Map<String, Map<String, SystemParamData>> sysParams,
			String code, String project) {
		return sysParams.get(project).get(code);
	}

	/**
	 * 获取字典表列表
	 * 
	 * @param dictionarys
	 * @param code
	 * @param project
	 * @return
	 */
	public static List<DictionaryData> findDictionaryByCode(Map<String, Map<String, List<DictionaryData>>> dictionarys,
			String code, String project) {
		return dictionarys.get(project).get(code);
	}

	/**
	 * 获取字典表列表
	 * 
	 * @param dictionarys
	 * @param code
	 * @return
	 */
	public static List<DictionaryData> findDictionaryByCode(Map<String, Map<String, List<DictionaryData>>> dictionarys,
			String code) {
		return findDictionaryByCode(dictionarys, code, getCurrProjectId());
	}

	
}
